package com.test.blog;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import com.jblog.core.model.blog.Category;
import com.jblog.core.model.blog.Post;
import com.jblog.core.model.blog.Tag;
import com.jblog.core.model.blog.param.PostParam;
import com.jblog.core.model.common.Privacy;
import com.jblog.core.model.common.Source;
import com.jblog.core.model.common.Status;
import com.jblog.core.service.facde.BlogFacade;

@Ignore
@ContextConfiguration("classpath:conf/spring/applicationContext.xml")
public class PostTest_cancel extends AbstractTransactionalJUnit4SpringContextTests {

    @Autowired BlogFacade blog;
    Post post;
    
    @Before
    public void setUp() throws Exception {
        Category catalog = new Category();
        catalog.setId(1L);
        
        post = new Post();
        post.setAuthor("leo");
        post.setAlias("abcde");
        post.setTitle("link test");
        post.setContent("link test");
        post.setScan(50L);
        post.setDescript("11111");
        post.setSource(Source.ORIGINAL);
        post.setStatus(Status.NORMAL);
        post.setIsAllowComment(true);
        post.setIsTop(false);
        post.setPrivacy(Privacy.PUBLIC);
        post.setLastUpdBy("leo");
        post.setCategory(catalog);
    }
    
    private Post createExistTagPost() {
        
        List<Tag> tags = new ArrayList<Tag>();
        Tag tag = new Tag();
        tag.setId(3L);
        tags.add(tag);
        tag = new Tag();
        tag.setId(2L);
        tag.setName("c技术");
        tag.setAlias("c-tech");
        tags.add(tag);
        
        post.setTags(tags);
        
        return post;
    }
    
    private Post createFixTagPost() {
        List<Tag> tags = new ArrayList<Tag>();
        Tag tag = new Tag();
        tag.setId(3L);
        tags.add(tag);
        tag = new Tag();
        tag.setName("美国");
        tag.setAlias("USA");
        tag.setStatus(Status.CANCEL);
        tags.add(tag);
        
        post.setTags(tags);
        
        return post;
    }
    
    private Post createNewTagPost() {
        List<Tag> tags = new ArrayList<Tag>();
        Tag tag = new Tag();
        tag.setName("中国");
        tag.setAlias("CN");
        tags.add(tag);
        tag = new Tag();
        tag.setName("美国");
        tag.setAlias("USA");
        tags.add(tag);
        
        post.setTags(tags);
        
        return post;
    }
    
    @Test
    public void testGetById() {
        Post post = this.createExistTagPost();
        blog.savePost(post);
        assertEquals("getById", post.getAlias(), "abcde");
        assertEquals("getById2", post.getTags().size(), 2);
    }
 
    @Test
    public void testGetByAlias() {
        Post post = this.createExistTagPost();
        blog.savePost(post);
        assertEquals("getById", post.getAlias(), "abcde");
        assertEquals("getById2", post.getTags().size(), 2);
        post = blog.findPostByAlisa("abcde");
        assertEquals("privacy", post.getPrivacy(), Privacy.PUBLIC);
    }
    
    @Test
    public void testExistTagSave() {
        Post post = this.createExistTagPost();
        blog.savePost(post);
    }
    @Test
    public void testFixTagSave() {
        Post post = this.createFixTagPost();
        blog.savePost(post);
    }
    @Test
    public void testNewTagSave() {
        Post post = this.createNewTagPost();
        blog.savePost(post);
    }
    
    @Test
    public void testExistTagUpdate() {
        Post post = this.createNewTagPost();
        blog.savePost(post);
        post = blog.findPostById(post.getId());
        post.setAuthor("kdu");
        blog.updatePost(post);
    }
    
    @Test
    public void testFixTagUpdate() {
        Post post = this.createExistTagPost();
        blog.savePost(post);
        post = blog.findPostById(6L);
        post.setAuthor("kdu");
        List<Tag> tags = post.getTags();
        tags.remove(0);
        
        List<Tag> ntags = new ArrayList<Tag>();
        ntags.add(tags.get(0));
        Tag tag = new Tag();
        tag.setName("英国");
        tag.setAlias("UK");
        ntags.add(tag);
        
        post.setTags(ntags);
        
        blog.updatePost(post);
    }
    
    @Test
//    @Ignore
    public void testTagUpdate_mysql() {

        Category catalog = new Category();
        catalog.setId(1L);
        catalog.setStatus(Status.NORMAL);
        
        post = new Post();
        post.setId(6L);
        post.setAuthor("leo");
        post.setAuthor("leo");
        post.setAlias("abcd");
        post.setTitle("link test");
        post.setContent("link test");
        post.setScan(50L);
        post.setDescript("11111");
        post.setSource(Source.ORIGINAL);
        post.setStatus(Status.NORMAL);
        post.setIsAllowComment(true);
        post.setIsTop(false);
        post.setPrivacy(Privacy.PUBLIC);
        post.setLastUpdBy("leo");
        post.setCategory(catalog);
        
        List<Tag> tags = new ArrayList<Tag>();
        Tag tag = new Tag();
        tag.setId(3L);
        tag.setName("jsp");
        tag.setAlias("jsp");
        tag.setStatus(Status.NORMAL);
        tags.add(tag);
        tag = new Tag();
        tag.setId(2L);
        tag.setName("c技术");
        tag.setAlias("c-tech");
        tag.setStatus(Status.NORMAL);
        tags.add(tag);
        
        post.setTags(tags);

        post.setAuthor("ldu");
        tags = post.getTags();
        tags.remove(0);
        
        List<Tag> ntags = new ArrayList<Tag>();
        ntags.add(tags.get(0));
        tag = new Tag();
        tag.setName("得国");
        tag.setAlias("JM");
        tag.setStatus(Status.NORMAL);
        ntags.add(tag);
        
        post.setTags(ntags);
        
        blog.updatePost(post);
    }

    @Test
    public void testGetByParam() {
        PostParam param = new PostParam(1, 2);
//        param.setIsTop(true);
//        param.setOrder(PostOrder.LAST_UPDATE_TIME);
        param.setSource(Source.RESHIPMENT);
        List<Post> posts = blog.findPostByParam(param);
        assertEquals(1, posts.size());
    }

    @Test
    public void testUpdate() {
        Category catalog = new Category();
        catalog.setId(1L);
        catalog.setStatus(Status.NORMAL);
        
        post = new Post();
        post.setId(6L);
//        post.setAuthor("leo");
//        post.setAuthor("leo");
//        post.setAlias("abcd");
//        post.setTitle("link test");
//        post.setContent("link test");
//        post.setScan(150L);
//        post.setDescript("11111");
//        post.setSource(Source.ORIGINAL);
//        post.setStatus(Status.NORMAL);
//        post.setIsAllowComment(true);
//        post.setIsTop(false);
        post.setPrivacy(Privacy.PRIVATE);
//        post.setLastUpdBy("leo");
//        post.setCatalog(catalog);
        
        List<Tag> tags = new ArrayList<Tag>();
        Tag tag = new Tag();
        tag.setId(3L);
        tag.setName("jsp");
        tag.setAlias("jsp");
        tag.setStatus(Status.NORMAL);
        tags.add(tag);
        tag = new Tag();
        tag.setId(2L);
        tag.setName("c技术");
        tag.setAlias("c-tech");
        tag.setStatus(Status.NORMAL);
        tags.add(tag);
        
        post.setTags(tags);

        post.setAuthor("ldu");
        tags = post.getTags();
        tags.remove(0);
        
        List<Tag> ntags = new ArrayList<Tag>();
        ntags.add(tags.get(0));
        tag = new Tag();
        tag.setName("得国");
        tag.setAlias("JM");
        tag.setStatus(Status.NORMAL);
        ntags.add(tag);
        
        post.setTags(ntags);
        
        blog.updatePost(post);
    }
}
